<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SSE Client</title>
</head>
<body>
<h1>SSE Example</h1>
<div id="messages"></div>
<script>
  // 客户端连接时携带 ID
  const clientId = "971019"; // 可替换为动态获取的 ID

  const eventSource = new EventSource(`http://localhost:9091/events?id=${clientId}&device_id=${new Date().getTime()}`);
  // const eventSource = new EventSource(`https://sse.waisongbang.com/events?id=${clientId}&device_id=${new Date().getTime()}`);

  // 接收服务器发送的消息
  eventSource.onmessage = function (event) {
    console.log("Message received:", event.data);
    if (event.data) {
      // 显示消息到页面
      const messageDiv = document.getElementById("messages");
      const newMessage = document.createElement("p");
      newMessage.textContent = event.data;
      messageDiv.appendChild(newMessage);
    }
  };

  eventSource.addEventListener('error', (event) => {
    console.log(event, 'event')
    eventSource.close()
  })

  eventSource.addEventListener('close', (event) => {
    console.log(event, 'close')
    eventSource.close()
  })
</script>
</body>
</html>
